#include "ASHyperlink.h"

static as_ret as_ctrl_hyperlink_set_url(as_control* ctrl_p, ASCHAR *url)
{
    as_ret ret = AS_OK;
    as_hyperlink* hl_p = (as_hyperlink*)ctrl_p;

    if(!hl_p || !url || as_strlen(url) <= 0)
    {
        ret = AS_INVALID_PARAM;
        return ret;
    }

    as_wstrcpy(hl_p->url, as_ucs2_from_gb2312(url));
    return ret;
}

static void as_ctrl_hyperlink_action(as_control* ctrl_p)
{
    as_hyperlink* hl_p = (as_hyperlink*)ctrl_p;

    if(!hl_p || as_wstrlen(hl_p->url) <= 0 || hl_p->state != AS_HL_STATE_CLICKED)
        return;

    //ret = vm_open_wap_url(hl_p->url);
    //vm_exit_app();//quit app
}

static as_ret as_ctrl_hyperlink_change_status(as_control* ctrl_p, as_hyperlink_state state)
{
    as_ret ret = AS_OK;
    as_hyperlink* hl_p = (as_hyperlink*)ctrl_p;

    if(!ctrl_p || state < AS_HL_STATE_NORMAL || state >= AS_HL_STATE_MAX)
    {
        ret = AS_INVALID_PARAM;
        return ret;
    }

    if(state == hl_p->state)
    {
        ret = AS_IGNORE;
        return ret;
    }

    hl_p->state = state;
    hl_p->base.paint_hdlr((as_control*)hl_p);
    as_ctrl_flush();
    return ret;
}

static as_ret as_ctrl_hyperlink_key_hdlr(as_control* ctrl_p, ASINT event, ASINT keycode)
{
    as_ret ret = AS_OK;
    as_hyperlink* hl_p = (as_hyperlink*)ctrl_p;

    if(!ctrl_p)
    {
        ret = AS_INVALID_PARAM;
        return ret;
    }

    if(ctrl_p->if_hidden)
        return AS_IGNORE;

    if(hl_p->state != AS_HL_STATE_SELECTED || keycode != AS_KEY_OK)
    {
        ret = AS_IGNORE;
        return ret;
    }

    //hl_p->state == AS_HL_STATE_SELECTED
    if(event == AS_EVT_KEY_DOWN)
    {
        return hl_p->change_status(ctrl_p, AS_HL_STATE_CLICKED);
    }
    else if (event == AS_EVT_KEY_UP)
    {
        hl_p->action((as_control*)hl_p);
        return AS_OK;
    }
    else
    {
        ret = AS_IGNORE;
        return ret;
    }
}

static as_ret as_ctrl_hyperlink_pen_hdlr(as_control* ctrl_p, ASINT event, ASINT x, ASINT y)
{
    as_hyperlink* hl_p = (as_hyperlink*)ctrl_p;
    ASINT ret = AS_OK;

    if(!ctrl_p)
    {
        ret = AS_INVALID_PARAM;
        return ret;
    }

    if(ctrl_p->if_hidden)
        return AS_IGNORE;

    if(event == AS_EVT_TOUCH_DOWN && AS_POINT_IN_RECT(x, y, hl_p->base.area) && hl_p->state == AS_HL_STATE_NORMAL)
    {
        return AS_OK;
        //return hl_p->change_status(ctrl_p, AS_HL_STATE_CLICKED);
    }
    else if(event == AS_EVT_TOUCH_MOVE && !AS_POINT_IN_RECT(x, y, hl_p->base.area) && hl_p->state == AS_HL_STATE_CLICKED)
    {
        return hl_p->change_status(ctrl_p, AS_HL_STATE_NORMAL);
    }
    else if(event == AS_EVT_TOUCH_UP /*&& hl_p->state == AS_HL_STATE_CLICKED*/ && AS_POINT_IN_RECT(x, y, hl_p->base.area))
    {
        hl_p->change_status(ctrl_p, AS_HL_STATE_CLICKED);
        hl_p->action((as_control*)hl_p);
        return AS_OK;
    }
    else
    {
        return AS_IGNORE;
    }
}

static void as_ctrl_hyperlink_change_area(as_control* ctrl_p, as_rect area)
{
    as_hyperlink* hl_p = (as_hyperlink*)ctrl_p;

    if(!hl_p)
        return;

    memcpy(&hl_p->base.area, &area, sizeof(as_rect));

    if(hl_p->label)
        hl_p->label->base.change_area_hdlr((as_control*)hl_p->label, area);
}

static void as_ctrl_hyperlink_paint(as_control* ctrl_p)
{
    ASUINT normal_text_color = 0x0000ff;
    ASUINT bg_color = 0xffffff;
    ASUINT select_text_color = 0x86198a;
    ASUINT line_color = 0x0000ff;
    as_hyperlink* hl_p = (as_hyperlink*)ctrl_p;
    as_rect* rect_p = NULL;
    ASINT layer;
    //ASUINT8* paint_buf;

    if(!hl_p || ctrl_p->if_hidden)
        return;

    rect_p = &hl_p->base.area;
    layer = ctrl_p->show_hdlr;

    if(layer < 0 || as_set_layer_clip_ex(layer, rect_p) == AS_IGNORE)
        return;

    if(hl_p->state == AS_HL_STATE_NORMAL || hl_p->state == AS_HL_STATE_SELECTED)
    {
        hl_p->label->set_text_color((as_control*)hl_p->label, normal_text_color);
    }
    else if(hl_p->state == AS_HL_STATE_CLICKED)
    {
        hl_p->label->set_text_color((as_control*)hl_p->label, select_text_color);
    }

    hl_p->label->set_bg_hidden((as_control*)hl_p->label, TRUE);
    //hl_p->label->set_bg_color((as_control*)hl_p->label, bg_color);
    //hl_p->label->set_bg_frame_color((as_control*)hl_p->label, bg_color);
    //back ground
    //paint_buf = vm_graphic_get_layer_buffer(layer);
    //as_fill_rect(hl_p->base.paint_buf, rect_p, bg_color, bg_color);
    //if(hl_p->state == AS_HL_STATE_SELECTED)
    //{
    //   as_draw_h_line(hl_p->base.paint_buf, rect_p->x, rect_p->x + rect_p->w, rect_p->y, normal_text_color);
    //}
    //label
    hl_p->label->base.paint_hdlr((as_control*)hl_p->label);
    //line
    //if(hl_p->state == AS_HL_STATE_CLICKED)
    //{
    //    as_draw_h_line(hl_p->base.paint_buf, rect_p->x, rect_p->x + rect_p->w, rect_p->y, select_text_color);
    //}
    //else
    //{
    //    as_draw_h_line(hl_p->base.paint_buf, rect_p->x, rect_p->x + rect_p->w, rect_p->y, line_color);
    //}
    as_reset_layer_clip(layer);
}

static void as_ctrl_hyperlink_move(as_control* ctrl_p, ASINT x_off, ASINT y_off)
{
    as_hyperlink* hl_p = (as_hyperlink*)ctrl_p;

    if(!hl_p)
        return;

    hl_p->base.area.x += (ASINT16)x_off;
    hl_p->base.area.y += (ASINT16)y_off;

    if(hl_p->label)
    {
        hl_p->label->base.move((as_control*)hl_p->label, x_off, y_off);
    }
}

as_hyperlink* as_ctrl_hyperlink_create(as_rect area, ASWSTR text, ASCHAR* url)
{
    as_hyperlink* hl_p = NULL;
    ASUINT param[1];

    if(!text || as_wstrlen(text) <= 0 || !url)
    {
        return hl_p;
    }

    hl_p = as_calloc(sizeof(as_hyperlink));

    if(!hl_p)
    {
        return hl_p;
    }

    param[0] = (ASUINT)text;
    hl_p->label = (as_label*)as_ctrl_create(AS_CTRL_LABEL, area, 1, (void**)param);

    if(!hl_p->label)
    {
        as_free(hl_p);
        hl_p = NULL;
        return hl_p;
    }

    as_ctrl_init((as_control*)hl_p, area);
    hl_p->base.key_hdlr = as_ctrl_hyperlink_key_hdlr;
    hl_p->base.pen_hdlr = as_ctrl_hyperlink_pen_hdlr;
    hl_p->base.paint_hdlr = as_ctrl_hyperlink_paint;
    hl_p->base.move = as_ctrl_hyperlink_move;
    hl_p->base.change_area_hdlr = as_ctrl_hyperlink_change_area;
    hl_p->base.ctrl_type = AS_CTRL_HYPERLINK;
    hl_p->state = AS_HL_STATE_NORMAL;
    hl_p->change_status = as_ctrl_hyperlink_change_status;
    hl_p->set_url = as_ctrl_hyperlink_set_url;
    hl_p->action = as_ctrl_hyperlink_action;
    hl_p->set_url((as_control*)hl_p, url);
    hl_p->label->set_layout((as_control*)hl_p->label, AS_LABEL_TEXT_LEFT);
    return hl_p;
}

as_ret as_ctrl_hyperlink_destroy(as_hyperlink* ctrl_hl_p)
{
    as_ret ret = AS_OK;

    if(!ctrl_hl_p)
    {
        ret = AS_INVALID_PARAM;
        return ret;
    }

    if(ctrl_hl_p->label)
    {
        as_ctrl_destroy((as_control*)ctrl_hl_p->label);
        ctrl_hl_p->label = NULL;
    }

    as_free(ctrl_hl_p);
    ctrl_hl_p = NULL;
    return ret;
}